package fetcher

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
)

//获取网络数据模块
func Fetch(url string) ([]byte, error) {
	resp, err := http.Get(url)

	if err != nil {
		return nil, err
	}

	defer resp.Body.Close() //完成后执行关闭

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("wrong status code: %d ", resp.StatusCode)
	}

	//转码
	//e := determineEncoding(resp.Body)
	//utf8Reader := transform.NewReader(resp.Body, e.NewEncoder())
	utf8Reader := transform.NewReader(resp.Body, simplifiedchinese.GBK.NewDecoder())

	return ioutil.ReadAll(utf8Reader) //返回读取的网页信息

}
